//
//  LHYSignAlertView.m
//  DrawLine
//
//  Created by lhy on 2021/1/21.
//

#import "LHYDrawLineView.h"

@interface LHYDrawLineView ()

@property (nonatomic, assign) float lineWidth;

@end

@implementation LHYDrawLineView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.7 alpha:0.6];
        self.layer.borderWidth = 1;
        self.layer.borderColor = UIColor.blueColor.CGColor;
    }
    return self;
}
#pragma mark - UI

- (void)show {
    UIWindow *window = [[UIApplication sharedApplication] windows].firstObject;
    [UIView animateWithDuration:0.2 animations:^{
        [window addSubview:self];
        self.center = window.center;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide {
    [UIView animateWithDuration:0.2 animations:^{
        [self removeFromSuperview];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)clear {
    [self.lineArr removeAllObjects];
    [self setNeedsDisplay];
}

- (void)saveSignImage {
    
    self.backgroundColor = UIColor.clearColor;
    self.layer.borderWidth = 0;
    
    UIGraphicsBeginImageContext(self.frame.size);
    
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    if (self.getSignImageBlock) {
        [self hide];
        self.getSignImageBlock(newImage);
    }
}

/// 绘制线段(核心)
/// @param rect rect
- (void)drawRect:(CGRect)rect {
    //    画线
    [self drawLine:self.lineArr];
}

#pragma 绘制线段(核心)
- (void)drawLine:(NSArray *)lineArr {
    
    if (lineArr.count) {
        
        [lineArr enumerateObjectsUsingBlock:^(NSArray *_Nonnull pointArr, NSUInteger idx, BOOL * _Nonnull stop) {
            //            上下文对象
            CGContextRef context = UIGraphicsGetCurrentContext();
            
            CGContextBeginPath(context);
            
            CGContextSetLineWidth(context, 2);
            
            CGPoint startPoint = [pointArr[0] CGPointValue];
            
            CGContextMoveToPoint(context, startPoint.x, startPoint.y);
            //            大于1个点时开始绘线
            if (pointArr.count > 1) {
                
                [pointArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    CGPoint linePoint = [pointArr[idx] CGPointValue];
                    CGContextAddLineToPoint(context, linePoint.x, linePoint.y);
                }];
            }
            //            绘线
            CGContextStrokePath(context);
        }];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    self.pointArr = [[NSMutableArray alloc] init];
    //    第一个点
    [self.pointArr addObject:[NSValue valueWithCGPoint:[touches.anyObject locationInView:self]]];
    //    第一条线
    [self.lineArr addObject:self.pointArr];
    
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //    移动中的点
    [self.pointArr addObject:[NSValue valueWithCGPoint:[touches.anyObject locationInView:self]]];
    //    重绘
    [self setNeedsDisplay];
}

- (NSMutableArray *)lineArr {
    if (!_lineArr) {
        _lineArr = [[NSMutableArray alloc] init];
    }
    return _lineArr;
}

@end




//    简单绘线
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetLineWidth(context, 3);
//    CGContextBeginPath(context);
//    CGContextMoveToPoint(context, 0, 0);
//
//    CGContextAddLineToPoint(context, 50, 100);
//    CGContextAddLineToPoint(context, 100, 110);
//
//    CGContextStrokePath(context);
